package com.webserver.http;

import org.omg.CORBA.StringHolder;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class HttpServletRequest {
    private Socket socket;
    //请求行信信息
    private String method;//方法
    private String url;//路径
    private String protocol;//协议

    private String requestURI;//uri中的请求部分，即:"?"左侧内容
    private String queryString;//uri中的参数部分，即:"?"右侧内容
    private Map<String,String> parameters = new HashMap<>();

    //消息头的相关信息
    Map<String, String> headers = new HashMap<>();

    public HttpServletRequest(Socket socket) throws IOException, EmptyRequestException {
        this.socket = socket;
        //1解析请求行
        parseRequestLine();
        //2解析消息头
        parseHeaders();
        //3解析消息正文
        parseContent();

    }

    //解析请求行
    private void parseRequestLine() throws IOException, EmptyRequestException {
        String line = readline();
        if(line.isEmpty()){
           throw new EmptyRequestException();
        }
        System.out.println("请求行内容:" + line);
        String[] data = line.split("\\s");//以碰到的每个空格划分，分隔后返回字符数组;
        method = data[0];
        url = data[1];
        protocol = data[2];

        parseURI();//进一步解析uri

        System.out.println("method:" + method);
        System.out.println("url:" + url);
        System.out.println("protocol:" + protocol);
    }

    private void parseURI(){
        String[] data = url.split("\\s");
        requestURI = data[0];
        if (data.length>1){
            queryString = data[1];
            data = queryString.split("&");
            for (String para : data){
                String[] arr = para.split("=",2);
                parameters.put(arr[0],arr[1]);
            }
        }
        System.out.println("requestURI:"+requestURI);
        System.out.println("queryString:"+queryString);
        System.out.println("paramenters:"+parameters);
    }
    //解析消息头
    private void parseHeaders() throws IOException {
        while (true) {
            String line = readline();
            if (line.isEmpty()) {//CRLF
                break;
            }
            System.out.println("消息头:" + line);
            String[] data = line.split(":\\s");
            headers.put(data[0], data[1]);
        }
        System.out.println("headers:" + headers);
    }

    //解析消息正文
    private void parseContent() {}

    private String readline() throws IOException {
        InputStream in = socket.getInputStream();
        StringBuilder builder = new StringBuilder();
        char pre = 'a', cur = 'a';//pre上次字符，cur本次字符
        int d;
        while ((d = in.read()) != -1) {
            cur = (char) d;
            if (pre == 13 && cur == 10) {
                break;
            }
            builder.append(cur);
            pre = cur;
        }
        return builder.toString().trim();//转为string型,去掉两边空白
    }

    public String getMethod() {
        return method;
    }

    public String getUrl() {
        return url;
    }

    public String getProtocol() {
        return protocol;
    }

    //根据消息头的名字获取对应的值
    public String getHeader(String name) {
        return headers.get(name);
    }
    public String getRequestURI() {
        return requestURI;
    }

    public String getQueryString() {
        return queryString;
    }

    public String getParameter(String name) {
        return parameters.get(name);
    }

}
